Imports System.Windows.Media.Animation

Partial Public Class Window1
    Inherits System.Windows.Window

    Private WithEvents _storyBoard As Storyboard
    Private _brushNotHandled As Brush
    Private _brushHandled As Brush
    Private _queDoMeNext As Queue(Of AnimateEvent)

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub btnStartDemo_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnStartDemo.Click

        Me.AddHandler(Window.PreviewMouseLeftButtonDownEvent, New MouseButtonEventHandler(AddressOf StealMouseDown))
        Me.Cursor = Cursors.Wait

        Me.tbViewer.Text = "Viewing Events"
        Me.btnStartDemo.IsEnabled = False
        Me.btnViewLogicalTree.IsEnabled = False
        Me.btnViewVisualTree.IsEnabled = False

        Me.pbGridBubble.Value = 0
        Me.pbGridPreview.Value = 0
        Me.pbStackPanelBubble.Value = 0
        Me.pbStackPanelPreview.Value = 0
        Me.pbClassPreview.Value = 0
        Me.pbClassBubble.Value = 0
        Me.pbWindowBubble.Value = 0
        Me.pbWindowPreview.Value = 0
        Me.pbButtonBubble.Value = 0
        Me.pbButtonPreview.Value = 0

        Me.pbGridBubble.Visibility = Windows.Visibility.Hidden
        Me.pbGridPreview.Visibility = Windows.Visibility.Hidden
        Me.pbStackPanelBubble.Visibility = Windows.Visibility.Hidden
        Me.pbStackPanelPreview.Visibility = Windows.Visibility.Hidden
        Me.pbClassBubble.Visibility = Windows.Visibility.Hidden
        Me.pbClassPreview.Visibility = Windows.Visibility.Hidden
        Me.pbWindowBubble.Visibility = Windows.Visibility.Hidden
        Me.pbWindowPreview.Visibility = Windows.Visibility.Hidden
        Me.pbButtonBubble.Visibility = Windows.Visibility.Hidden
        Me.pbButtonPreview.Visibility = Windows.Visibility.Hidden

        Me.lbEventTracker.Items.Clear()
        _queDoMeNext = New Queue(Of AnimateEvent)

        Dim lbi As New ListBoxItem
        lbi.Content = "Start Demo Clicked"
        lbi.Background = New System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.BlanchedAlmond)
        lbi.Foreground = New System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black)
        lbi.FontWeight = System.Windows.FontWeights.Bold
        Me.lbEventTracker.Items.Add(lbi)


        If Me.cbWindowPreviewHandler.IsChecked.Value = True Then
            Me.AddHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Window_PreviewTrace), True)
        End If

        If Me.cbWindowBubbleHandler.IsChecked.Value = True Then
            Me.AddHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Window_Trace), True)
        End If

        If Me.cbGridPreviewHandler.IsChecked.Value = True Then
            Me.gridLayout.AddHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Grid_PreviewTrace), True)
        End If

        If Me.cbGridBubbleHandler.IsChecked.Value = True Then
            Me.gridLayout.AddHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Grid_Trace), True)
        End If

        If Me.cbStackPanelPreviewHandler.IsChecked.Value = True Then
            Me.stackPanelForButton.AddHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf StackPanel_PreviewTrace), True)
        End If

        If Me.cbStackPanelBubbleHandler.IsChecked.Value = True Then
            Me.stackPanelForButton.AddHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf StackPanel_Trace), True)
        End If

        If Me.cbButtonPreviewHandler.IsChecked.Value = True Then
            Me.btnStartDemo.AddHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf btnStartDemo_PreviewGenerateTrace))
        End If

        If Me.cbButtonBubbleHandler.IsChecked.Value = True Then
            Me.btnStartDemo.AddHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf btnStartDemo_GenerateTrace))
        End If

    End Sub

    Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        _storyBoard = CType(Me.FindResource("animateValueTimeline"), Storyboard)
        _brushNotHandled = CType(Me.FindResource("NotHandledBrush"), Brush)
        _brushHandled = CType(Me.FindResource("HandledBrush"), Brush)

        EventManager.RegisterClassHandler(GetType(TraceRoutedEventButton), TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf ClassHandler_PreviewTrace), True)
        EventManager.RegisterClassHandler(GetType(TraceRoutedEventButton), TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf ClassHandler_BubbleTrace), True)

    End Sub

    Private Sub ClassHandler_PreviewTrace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)

        'this is here because once you add a Class handler, I could not find a way to remove it.
        'so this is a workaround so that the UI works properly
        If Me.cbClassPreviewHandler.IsChecked.Value = True Then
            _queDoMeNext.Enqueue(New AnimateEvent(Me.pbClassPreview, e.Handled, "Class Tunnel"))

            If Me.rdoClassPreview.IsChecked.Value = True Then
                e.Handled = True
            End If

        End If
    End Sub

    Private Sub ClassHandler_BubbleTrace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        'this is here because once you add a Class handler, I could not find a way to remove it.
        'so this is a workaround so that the UI works properly
        If Me.cbClassBubbleHandler.IsChecked.Value = True Then

            _queDoMeNext.Enqueue(New AnimateEvent(Me.pbClassBubble, e.Handled, "Class Bubble"))

            If Me.rdoClassBubble.IsChecked.Value = True Then
                e.Handled = True
            End If

        End If
    End Sub

    Private Sub Window_PreviewTrace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbWindowPreview, e.Handled, "Window Tunnel"))

        If Me.rdoWindowPreview.IsChecked.Value = True Then
            e.Handled = Me.rdoWindowPreview.IsChecked.Value
        End If

    End Sub

    Private Sub Grid_PreviewTrace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbGridPreview, e.Handled, "Grid Tunnel"))

        If Me.rdoGridPreview.IsChecked.Value = True Then
            e.Handled = True
        End If

    End Sub

    Private Sub StackPanel_PreviewTrace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbStackPanelPreview, e.Handled, "Stack Tunnel"))

        If Me.rdoStackPanelPreview.IsChecked.Value = True Then
            e.Handled = True
        End If

    End Sub

    Private Sub Window_Trace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbWindowBubble, e.Handled, "Window Bubble"))
    End Sub

    Private Sub Grid_Trace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbGridBubble, e.Handled, "Grid Bubble"))

        If Me.rdoGridBubble.IsChecked.Value = True Then
            e.Handled = True
        End If

    End Sub

    Private Sub StackPanel_Trace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbStackPanelBubble, e.Handled, "Stack Bubble"))

        If Me.rdoStackPanelBubble.IsChecked.Value = True Then
            e.Handled = True
        End If

    End Sub

    Private Sub btnStartDemo_GenerateTrace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbButtonBubble, e.Handled, "Button Bubble"))

        If Me.rdoButtonBubble.IsChecked.Value = True Then
            e.Handled = True
        End If

    End Sub

    Private Sub StealMouseDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
        e.Handled = True
    End Sub

    Private Sub btnStartDemo_GenerateTraceCompleted(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStartDemo.GenerateTraceCompleted
        ProcessNextLongRunningJobAnimation(Nothing, Nothing)
    End Sub

    Private Sub btnStartDemo_PreviewGenerateTrace(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        _queDoMeNext.Enqueue(New AnimateEvent(Me.pbButtonPreview, e.Handled, "Button Tunnel"))

        If Me.rdoButtonPreview.IsChecked.Value = True Then
            e.Handled = True
        End If

    End Sub

    Private Sub AnimateProgressBar(ByVal pb As ProgressBar, ByVal bolHasBeenHandled As Boolean, ByVal strMessage As String)

        pb.Visibility = Windows.Visibility.Visible

        If bolHasBeenHandled = False Then
            pb.Foreground = _brushNotHandled

        Else
            pb.Foreground = _brushHandled
        End If

        Dim lbi As New ListBoxItem
        lbi.Content = strMessage
        lbi.Foreground = pb.Foreground
        lbi.FontWeight = System.Windows.FontWeights.Bold
        Me.lbEventTracker.Items.Add(lbi)
        Storyboard.SetTargetName(_storyBoard, pb.Name)
        _storyBoard.Begin(Me)

    End Sub

    Private Sub ProcessNextLongRunningJobAnimation(ByVal sender As Object, ByVal e As System.EventArgs) Handles _storyBoard.Completed

        If _queDoMeNext.Count > 0 Then

            Dim obj As AnimateEvent = _queDoMeNext.Dequeue
            AnimateProgressBar(obj.AnimateProgressBar, obj.Handled, obj.Message)

        Else
            Me.btnStartDemo.IsEnabled = True
            Me.btnViewLogicalTree.IsEnabled = True
            Me.btnViewVisualTree.IsEnabled = True

            Dim lbi As New ListBoxItem
            lbi.Content = "All Events Handled"
            lbi.Background = New System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.BlanchedAlmond)
            lbi.Foreground = New System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Black)
            lbi.FontWeight = System.Windows.FontWeights.Bold
            Me.lbEventTracker.Items.Add(lbi)
            Me.Cursor = Cursors.Arrow
            Me.RemoveHandler(Window.PreviewMouseLeftButtonDownEvent, New MouseButtonEventHandler(AddressOf StealMouseDown))

            If Me.cbWindowPreviewHandler.IsChecked.Value = True Then
                Me.RemoveHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Window_PreviewTrace))
            End If

            If Me.cbWindowBubbleHandler.IsChecked.Value = True Then
                Me.RemoveHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Window_Trace))
            End If

            If Me.cbGridPreviewHandler.IsChecked.Value = True Then
                Me.gridLayout.RemoveHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Grid_PreviewTrace))
            End If

            If Me.cbGridBubbleHandler.IsChecked.Value = True Then
                Me.gridLayout.RemoveHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf Grid_Trace))
            End If

            If Me.cbStackPanelPreviewHandler.IsChecked.Value = True Then
                Me.stackPanelForButton.RemoveHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf StackPanel_PreviewTrace))
            End If

            If Me.cbStackPanelBubbleHandler.IsChecked.Value = True Then
                Me.stackPanelForButton.RemoveHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf StackPanel_Trace))
            End If

            If Me.cbButtonPreviewHandler.IsChecked.Value = True Then
                Me.btnStartDemo.RemoveHandler(TraceRoutedEventButton.PreviewGenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf btnStartDemo_PreviewGenerateTrace))
            End If

            If Me.cbButtonBubbleHandler.IsChecked.Value = True Then
                Me.btnStartDemo.RemoveHandler(TraceRoutedEventButton.GenerateTraceRoutedEvent, New RoutedEventHandler(AddressOf btnStartDemo_GenerateTrace))
            End If

        End If

    End Sub

    Private Sub btnViewLogicalTree_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnViewLogicalTree.Click
        lbEventTracker.Items.Clear()
        Me.tbViewer.Text = "Logical Tree"

        Dim objDependencyObject As DependencyObject = Me.btnStartDemo

        While (objDependencyObject) IsNot Nothing
            lbEventTracker.Items.Insert(0, objDependencyObject.ToString)
            objDependencyObject = LogicalTreeHelper.GetParent(objDependencyObject)
        End While

    End Sub

    Private Sub btnViewVisualTree_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnViewVisualTree.Click
        lbEventTracker.Items.Clear()
        Me.tbViewer.Text = "Visual Tree"

        Dim objDependencyObject As DependencyObject = Me.btnStartDemo

        While (objDependencyObject) IsNot Nothing
            lbEventTracker.Items.Insert(0, objDependencyObject.ToString)
            objDependencyObject = VisualTreeHelper.GetParent(objDependencyObject)
        End While

    End Sub

    Private Sub cbButtonBubbleHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbButtonBubbleHandler.Click
        Me.rdoButtonBubble.IsEnabled = Me.cbButtonBubbleHandler.IsChecked.Value
        If Me.cbButtonBubbleHandler.IsChecked.Value = False Then
            Me.rdoButtonBubble.IsChecked = False
        End If
    End Sub

    Private Sub cbButtonPreviewHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbButtonPreviewHandler.Click
        Me.rdoButtonPreview.IsEnabled = Me.cbButtonPreviewHandler.IsChecked.Value
        If Me.cbButtonPreviewHandler.IsChecked.Value = False Then
            Me.rdoButtonPreview.IsChecked = False
        End If
    End Sub

    Private Sub cbClassBubbleHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbClassBubbleHandler.Click
        Me.rdoClassBubble.IsEnabled = Me.cbClassBubbleHandler.IsChecked.Value
        If Me.cbClassBubbleHandler.IsChecked.Value = False Then
            Me.rdoClassBubble.IsChecked = False
        End If
    End Sub

    Private Sub cbClassPreviewHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbClassPreviewHandler.Click
        Me.rdoClassPreview.IsEnabled = Me.cbClassPreviewHandler.IsChecked.Value
        If Me.cbClassPreviewHandler.IsChecked.Value = False Then
            Me.rdoClassPreview.IsChecked = False
        End If
    End Sub

    Private Sub cbGridBubbleHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbGridBubbleHandler.Click
        Me.rdoGridBubble.IsEnabled = Me.cbGridBubbleHandler.IsChecked.Value
        If Me.cbGridBubbleHandler.IsChecked.Value = False Then
            Me.rdoGridBubble.IsChecked = False
        End If
    End Sub

    Private Sub cbGridPreviewHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbGridPreviewHandler.Click
        Me.rdoGridPreview.IsEnabled = Me.cbGridPreviewHandler.IsChecked.Value
        If Me.cbGridPreviewHandler.IsChecked.Value = False Then
            Me.rdoGridPreview.IsChecked = False
        End If
    End Sub

    Private Sub cbStackPanelBubbleHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbStackPanelBubbleHandler.Click
        Me.rdoStackPanelBubble.IsEnabled = Me.cbStackPanelBubbleHandler.IsChecked.Value
        If Me.cbStackPanelBubbleHandler.IsChecked.Value = False Then
            Me.rdoStackPanelBubble.IsChecked = False
        End If
    End Sub

    Private Sub cbStackPanelPreviewHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbStackPanelPreviewHandler.Click
        Me.rdoStackPanelPreview.IsEnabled = Me.cbStackPanelPreviewHandler.IsChecked.Value
        If Me.cbStackPanelPreviewHandler.IsChecked.Value = False Then
            Me.rdoStackPanelPreview.IsChecked = False
        End If
    End Sub

    Private Sub cbWindowPreviewHandler_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbWindowPreviewHandler.Click
        Me.rdoWindowPreview.IsEnabled = Me.cbWindowPreviewHandler.IsChecked.Value
        If Me.cbWindowPreviewHandler.IsChecked.Value = False Then
            Me.rdoWindowPreview.IsChecked = False
        End If
    End Sub

    Private Sub lbEventTracker_PreviewMouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles lbEventTracker.PreviewMouseLeftButtonDown
        If TypeOf e.OriginalSource Is TextBlock Then
            e.Handled = True
        End If
    End Sub
End Class
